package com.opennetexam.manager.mapper;

import com.opennetexam.manager.databasemodel.ExamineeAnswerDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author 奶茶
 * Created by on 2017/12/13.
 */
@Mapper
public interface ExamineeAnswerDetailMapper {

    /**
     * 获取考生当前全部作答
     * @param examineeId
     * @param examPaperInfoId
     * @return
     */
    @Select("SELECT * FROM ExamineeAnswerDetail WHERE ExamineeId = #{examineeId} AND LOWER(ExamPaperID) = #{examPaperID}")
    List<Map<String,Object>> findExamineeAnswerDetails(@Param(value = "examineeId") String examineeId, @Param(value = "examPaperID") String examPaperInfoId);

    @Select("SELECT * FROM ExamineeAnswerDetail WHERE ExamineeId = #{examineeId} AND LOWER(ExamPaperInfoId) = #{examPaperInfoId} AND ExamPaperItemId = #{examPaperItemId} ORDER BY Id DESC LIMIT 1")
    ExamineeAnswerDetail findBeforeExamineeAnswerDetails(@Param(value = "examineeId") String examineeId, @Param(value = "examPaperInfoId") String examPaperInfoId,@Param(value = "examPaperItemId") String examPaperItemId);

    @Insert("INSERT INTO ExamineeAnswerDetail (\n" +
            "\tExamineeId,\n" +
            "\tExamPaperID,\n" +
            "\tExamPaperItemId,\n" +
            "\tSequenceIndex,\n" +
            "\tIsCompletion,\n" +
            "\tAnswerResults,\n" +
            "\tScore,\n" +
            "\tIsMarked,\n" +
            "\tCreateTime\n" +
            ")\n" +
            "VALUE\n" +
            "\t(\n" +
            "\t\t#{examineeId},\n" +
            "\t\t#{examPaperID},\n" +
            "\t\t#{examPaperItemId},\n" +
            "\t\t#{sequenceIndex},\n" +
            "\t\t#{isCompletion},\n" +
            "\t\t#{answerResults},\n" +
            "\t\t#{score},\n" +
            "\t\t#{isMarked},\n" +
            "\t\tNOW()\n" +
            "\t) ON DUPLICATE KEY UPDATE AnswerResults = #{answerResults}, IsCompletion = #{isCompletion}, IsMarked = #{isMarked}, CreateTime=NOW()")
    Integer addExamineeAnswerDetail(ExamineeAnswerDetail examineeAnswerDetail);

    @Update("UPDATE ExamineeAnswerDetail SET ExamineeId = #{examineeId},ExamPaperInfoId = #{examPaperID},ExamPaperItemId = #{examPaperID}," +
            "SequenceIndex = #{sequenceIndex},SequenceIndex = #{sequenceIndex},IsCompletion = #{isCompletion},AnswerResults = #{answerResults}," +
            "Score = #{score},IsMarked = #{isMarked} WHERE Id = #{id}")
    Integer updateExamineeAnswerDetail(ExamineeAnswerDetail examineeAnswerDetail);

}
